package com.zyf.audio;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * Created by hp on 2021/10/18.
 */
public class DruidTest {
    public static void main(String[] args) throws Exception{
        //数据源配置
        Properties properties=new Properties();
        //通过当前类的class对象获取资源文件
        InputStream is = DruidTest.class.getResourceAsStream("/druid.properties");
        properties.load(is);
        //返回的是DataSource，不是DruidDataSource
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        //获取连接
        Connection connection = dataSource.getConnection();

        //PreparedStatement接口
        String sql2 = "SELECT * FROM  device_view_new;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql2);
        ResultSet rs = preparedStatement.executeQuery();
        while (rs.next()){
            int db = rs.getInt("db");
            System.out.println(db);
        }
        preparedStatement.execute();

        //关闭连接
        connection.close();
    }
}
